package com.xzlf.service;

import com.alibaba.fastjson.JSON;
import com.xzlf.config.Constans;
import com.xzlf.dto.RequestDTO;
import com.xzlf.dto.ResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class AsyncSendService{
    @Autowired
    private DemoService demoService;


    public void send(RequestDTO requestDTO) {
        String reqeustID = requestDTO.getRequestId();
        new Thread(() ->{
            int count = requestDTO.getCount();
            String name = Thread.currentThread().getName();
            log.info("========异步请求：{} 启动==============", name);
            Constans.REENTRANT_LOCK.lock();
            ResultDTO rt = new ResultDTO();
            rt.setImgCount(count);
            rt.setMsg(MessageFormat.format("完成{0}张图片的异步请求", count));
            try {
                while (count > 0) {
                    while (demoService.getBuffersize() > 0) {
                        Constans.LISTENERUN.signal();
                        log.info("========异步定损请求：{} 准备给同步请求让路，请 listener 监听同步线程进展==============", name);
                        Constans.ASYNCRUN.await();
                    }
                    log.info("========异步定损线程：{} 发送图片：{}==============", name, count);
                    count--;
                    TimeUnit.SECONDS.sleep(1);
                }
                rt.setResponseDate(new Date());
                rt.setResult("analysis success");
                demoService.setResult(reqeustID, JSON.toJSONString(rt));
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                Constans.REENTRANT_LOCK.unlock();
            }
        }, "异步透传分析线程--"+reqeustID).start();
    }
    public void up(RequestDTO requestDTO) {
        String reqeustID = requestDTO.getRequestId();
        new Thread(() ->{
            int count = requestDTO.getCount();
            String name = Thread.currentThread().getName();
            log.info("========异步请求：{} 启动==============", name);
            Constans.REENTRANT_LOCK_UP.lock();
            ResultDTO rt = new ResultDTO();
            rt.setImgCount(count);
            rt.setMsg(MessageFormat.format("完成{0}张图片的异步请求", count));
            try {
                while (count > 0) {
                    while (demoService.getBuffersize() > 0) {
                        Constans.LISTENEUP.signal();
                        log.info("========异步识别请求：{} 准备给同步请求让路，请 listener 监听同步线程进展==============", name);
                        Constans.ASYNCUP.await();
                    }
                    log.info("========异步识别线程：{} 发送图片：{}==============", name, count);
                    count--;
                    TimeUnit.SECONDS.sleep(1);
                }
                rt.setResponseDate(new Date());
                rt.setResult("up success");
                demoService.setResult(reqeustID, JSON.toJSONString(rt));
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                Constans.REENTRANT_LOCK_UP.unlock();
            }
        }, "异步部位识别线程--"+reqeustID).start();
    }


    public ResultDTO getResult(String requestId){
        String result = (String) demoService.getResult(requestId);

        ResultDTO resultDTO = JSON.parseObject(result, ResultDTO.class);
        return resultDTO;
    }
}
